<style type="text/css">
        .layui-table-cell {
            height: auto;
            line-height: 28px;
        }
    </style>
<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">会员服务</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>会员服务</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="user-search-key">
                <option value="">-请选择-</option>
                <option value="customer_name">顾客姓名</option>
                <option value="card_name">会员卡类型</option>
                <option value="customer_phone">顾客手机号</option>
            </select>&emsp;
            <input id="user-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="user-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="btn-reset" class="layui-btn icon-btn"><i class="layui-icon">&#x1006;</i>重置</button>
        </div>

        <table class="layui-table" id="payCard-table" lay-filter="payCard-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="payCard-table-bar">
    <% if(add) { %><a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="record">消费记录</a><% } %>
    <% if(record) { %><a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="payButton">会员服务</a><% } %>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="cardName-tpl-state">
	{{# if (d.cardName=== '1') { }}  
		<button class="layui-btn layui-btn-xs layui-btn-normal">脸卡</button>
	{{# } else if(d.cardName=== '2') { }}  
		<button class="layui-btn layui-btn-xs layui-btn-danger">颈卡</button>
	{{# } }}
</script>
<!-- 表格状态列 -->
<script type="text/html" id="cardType-tpl-state">
	{{# if (d.cardType=== 1) { }}  
		<button class="layui-btn layui-btn-xs layui-btn-normal">次数卡</button>
	{{# } else if(d.cardType=== 2) { }}  
		<button class="layui-btn layui-btn-xs layui-btn-danger">充值卡</button>
	{{# } }}
</script>
<!-- 表格状态列 -->
<script type="text/html" id="isActive-tpl-state">
	{{# if (d.isActive=== 1) { }}  
		<button class="layui-btn layui-btn-xs layui-btn-normal">有效</button>
	{{# } else if(d.isActive=== 0) { }}  
		<button class="layui-btn layui-btn-xs layui-btn-danger">无效</button>
	{{# } }}
</script>
<script>
    layui.use(['form', 'table', 'util', 'admin', 'element'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var element = layui.element;

        form.render('select');

        // 渲染表格
        
        table.render({
            elem: '#payCard-table',
            url: 'system/payCard/list',
            where: {shopId: $("#shopInfo").attr("data")},
            page: true,
            cols: [[
                {type: 'numbers',title: '序号'},
                {field: 'customerName', title: '顾客姓名'},
                {field: 'customerPhone', title: '顾客手机号'},
                {field: 'cardName', title: '会员卡类型'},
                {field: 'serviceTimes', title: '剩余次数'},
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                } ,
                {align: 'center', toolbar: '#payCard-table-bar', title: '操作'} 
            ]]
        });

        // 添加按钮点击事件
        $('#shop-btn-add').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(payCard-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'payButton') { // 修改
                showEditModel(data);
            } else if (layEvent === 'record') {//查看详情记录
            	showDetailModel(data.id);
            } else if (layEvent === 'del') { // 删除
				layer.confirm('确定要删除吗？', function() {
					layer.load(2);
					admin.req("service/shop/delete", {
						id : obj.data.id
					}, function(data) {
						layer.closeAll('loading');
						if (data.code == 1) {
							layer.msg(data.msg, {
								icon : 1
							});
							obj.del();
						} else {
							layer.msg(data.msg, {
								icon : 2
							});
						}
					}, "post");
				});
            }
        });

        // 显示表单弹窗
        var showEditModel = function (data) {
            var title = '会员服务';
            admin.putTempData('t_payCard', data);
            var str =  data ? JSON.stringify(data) : " ";
            admin.popupCenter({
            	area: '40%',
            	offset: '10%',
                title: title,
                path: "system/payCard/editForm?shopId="+$("#shopInfo").attr("data"),
                data:JSON.stringify(data),
                finish: function () {
                	location.reload();
                },
                cancel: function(index, layero){ 
                	location.reload();
                }    
            });
        };

        // 搜索按钮点击事件
        $('#user-btn-search').click(function () {
            var key = $('#user-search-key').val();
            var value = $('#user-search-value').val();
            table.reload('payCard-table', {where: {searchKey: key, searchValue: value}});
        });

        
        // 重置按钮
		$('#btn-reset').click(function() {
			location.reload();
		});
        
		// 显示表单弹窗
		var showDetailModel = function(id) {
			admin.popupCenter({
				area : '50%',
				title : "会员服务记录",
				path : "system/payCard/record?id="+id,
				finish : function() {
					location.reload();
				}
			});
		};
		
    });
</script>